package org.lord.slark.numbers.utils;

import android.util.Xml;

import org.lord.slark.numbers.entity.Scripture;
import org.xmlpull.v1.XmlPullParser;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Slark on 2016/5/29.
 */
public class BibleTools {
    public static List<Scripture> getScriptures(InputStream xml) throws Exception {
        List<Scripture> scriptures = null;
        Scripture scripture = null;
        XmlPullParser pullParser = Xml.newPullParser();
        pullParser.setInput(xml, "UTF-8");
        int event = pullParser.getEventType();
        while (event != XmlPullParser.END_DOCUMENT) {
            switch (event) {
                case XmlPullParser.START_DOCUMENT:
                    scriptures = new ArrayList<Scripture>();
                    break;
                case XmlPullParser.START_TAG:
                    if (Scripture.COLUMN_SCRIPTURE.equals(pullParser.getName())) {
                        scripture = new Scripture();
                    }
                    if (Scripture.COLUMN_ID.equals(pullParser.getName())) {
                        scripture.setId(Integer.valueOf(pullParser.nextText()));
                    }
                    if (Scripture.COLUMN_ORIGIN.equals(pullParser.getName())) {
                        scripture.setOrigin(pullParser.nextText());
                    }
                    if (Scripture.COLUMN_CONTENT.equals(pullParser.getName())) {
                        scripture.setContent(pullParser.nextText());
                    }
                    break;
                case XmlPullParser.END_TAG:
                    if (Scripture.COLUMN_SCRIPTURE.equals(pullParser.getName())) {
                        scriptures.add(scripture);
                        scripture = null;
                    }
                    break;
            }
            event = pullParser.next();
        }
        return scriptures;
    }
}
